<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2011
 */

function evalQuery(&$query, $test) {
	if (!isset($query) || strlen(trim($query)) === 0)
		return;
	$orig = $query;

	$s = strpos($query, "(");
	$f = 1;
	while ($s > 0 && $f > 0) {
		if ($s > 0) {
			$f = strpos($query, ")", $s);
			if ($f > 0)
				$query = substr($query, 0, $s - 1) . strtoupper(evalQuery(substr($query, $s + 1, $f - $s -1), $test)) . substr($query, $f + 1);
		}

		$s = strpos($query, "(");
	}

	//remove any invalid characters
	$query = str_replace("?", "", $query);
	$query = str_replace("  ", " ", $query);
	$query = str_replace("\r", "", $query);
	$query = str_replace("\n", "", $query);
	$query = str_replace("\t", "", $query);
	$query = str_replace(".", "", $query);
	$query = str_replace("(", "", $query);
	$query = str_replace(")", "", $query);

	$i = explode(" ", $query);

	if (strpos($query, "\"", 1)) {
		$len = count($i);
		for ($a = $len - 1; $a >= 0; $a--) {
			if (substr($i[$a], -1) === "\"" && substr($i[$a], 0, 1) !== "\"") {
				$i[$a - 1] = $i[$a - 1] . " ". $i[$a];
				$i[$a] = "";
			}

			if (substr($i[$a], -1) === "\"" && substr($i[$a], 0, 1) === "\"" && strlen($i[$a]) > 1)
				$i[$a] = "!" . substr($i[$a], 1, strlen($i[$a]) - 2);
		}

		$j = $i;
		$b = 0;
		//remove nulls
		for ($a = 0; $a < $len; $a++) {
			if ($i[$a] !== "") {
				$j[$b] = $i[$a];
				++$b;
			}
			else {
				$j[$b] = "";
			}
		}

		$i = $j;
	}

	//now, do the actual searching for each string
	$len = count($i);
	for ($a = 0; $a < $len; $a++) {
		if ($i[$a] === "AND")
			$i[$a] = "AND";
		elseif ($i[$a] === "OR")
			$i[$a] = "OR";
		elseif ($i[$a] === "NOT")
			$i[$a] = "NOT";
		elseif ($i[$a] === "XOR")
			$i[$a] = "XOR";
		elseif ($i[$a] === "TRUE")
			$i[$a] = "true";
		elseif ($i[$a] === "FALSE")
			$i[$a] = "false";
		else {
			if (substr($i[$a], 0, 1) === "!")
				$i[$a] = substr($i[$a], 1);
			$i[$a] = strtolower((boolean)(stripos($test, $i[$a], 1)));
		}
	}

	//we now have a boolean expression on our hands:
    //false AND true XOR false
    $query = implode(" ", $i);

    //this is cheating, I know, but, It's quicker then parsing out the
    //proper way, so...
    while(strpos($query, " ", 1)) {
		if (strpos($query, "NOT", 1) >= 0) {
			$query = str_replace("NOT false", "true", $query);
			$query = str_replace("NOT true", "false", $query);
		}

		if (strpos($query, "OR", 1) >= 0) {
			$query = str_replace("false OR false", "false", $query);
			$query = str_replace("false OR true", "true", $query);
			$query = str_replace("true OR false", "true", $query);
			$query = str_replace("true OR true", "true", $query);
		}

		if (strpos($query, "AND", 1) >= 0) {
			$query = str_replace("false AND false", "false", $query);
			$query = str_replace("false AND true", "false", $query);
			$query = str_replace("true AND false", "false", $query);
			$query = str_replace("true AND true", "false", $query);
		}

		if (strpos($query, "XOR", 1) >= 0) {
			$query = str_replace("false XOR false", "false", $query);
			$query = str_replace("false XOR true", "true", $query);
			$query = str_replace("true XOR false", "true", $query);
			$query = str_replace("true XOR true", "false", $query);
		}

		if (strpos($query, " ", 1) >= 0) {
			$query = str_replace("true true", "true", $query);
			$query = str_replace("false true", "true", $query);
			$query = str_replace("true false", "true", $query);
			$query = str_replace("false false", "false", $query);
		}

		$query = $orig;
	}
}

$s = "Toi di tom va nung. No lai mot con noi nua, nhung no lai dien";
$q = "tom and dien";

if (evalQuery($q, $s)) {
	echo ("Ngon roi");
}
?>